package com.cs.bd.utils;

import android.text.TextUtils;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.json.JSONException;
import org.json.JSONObject;

public class StringUtils
{
    private static final String DEFAULT_CHARSET_UTF8 = "UTF-8";

    public static String toUpperCase(Object obj)
    {
        if (obj == null) {
            obj = "";
        }
        return obj.toString().trim().toUpperCase();
    }

    public static String toLowerCase(Object obj)
    {
        if (obj == null) {
            obj = "";
        }
        return obj.toString().trim().toLowerCase();
    }

    public static Integer toInteger(Object srcStr, Integer defaultValue)
    {
        try
        {
            if ((srcStr != null) && (isInt(srcStr)))
            {
                String s = srcStr.toString().replaceAll("(\\s)", "");
                return s.length() > 0 ? Integer.valueOf(s) : defaultValue;
            }
        }
        catch (Exception localException) {}
        return defaultValue;
    }

    public static Long toLong(Object srcStr, Long defaultValue)
    {
        try
        {
            if ((srcStr != null) && (isInt(srcStr)))
            {
                String s = srcStr.toString().replaceAll("(\\s)", "");
                return Long.valueOf(s.length() > 0 ? Long.parseLong(s) : defaultValue.longValue());
            }
        }
        catch (Exception localException) {}
        return defaultValue;
    }

    public static boolean isInt(Object srcStr)
    {
        if (srcStr == null) {
            return false;
        }
        String s = srcStr.toString().replaceAll("(\\s)", "");
        Pattern p = Pattern.compile("([-]?[\\d]+)");
        Matcher m = p.matcher(s);
        return m.matches();
    }

    public static String toString(Object obj)
    {
        if (obj == null) {
            obj = "";
        }
        return obj.toString().trim();
    }

    public static boolean isNumber(String str)
    {
        return str.matches("^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$");
    }

    public static String trim(Object srcStr)
    {
        if (srcStr != null) {
            return srcStr.toString().trim();
        }
        return null;
    }

    public static byte[] stringToByteArray(String src)
    {
        if (TextUtils.isEmpty(src)) {
            return null;
        }
        byte[] ret = null;
        try
        {
            ret = src.getBytes("UTF-8");
        }
        catch (UnsupportedEncodingException e)
        {
            e.printStackTrace();
        }
        return ret;
    }

    public static String byteArrayToString(byte[] src)
    {
        if (src == null) {
            return null;
        }
        String ret = null;
        try
        {
            ret = new String(src, "UTF-8");
        }
        catch (UnsupportedEncodingException e)
        {
            e.printStackTrace();
        }
        return ret;
    }

    public static byte[] jsonToByteArray(JSONObject json)
    {
        if (json == null) {
            return null;
        }
        byte[] ret = null;
        ret = stringToByteArray(json.toString());
        return ret;
    }

    public static JSONObject byteArrayToJson(byte[] src)
    {
        if (src == null) {
            return null;
        }
        JSONObject ret = null;
        String str = byteArrayToString(src);
        if (str == null) {
            return null;
        }
        try
        {
            ret = new JSONObject(str);
        }
        catch (JSONException e)
        {
            e.printStackTrace();
        }
        return ret;
    }

    public static String urlEncode(String url)
    {
        String reutrnUrl = url;
        try
        {
            if (url.indexOf('?') > 0)
            {
                String uri = url.substring(url.indexOf("://") + 3, url.indexOf("?"));
                uri = URLEncoder.encode(uri, "UTF-8");
                uri = uri.replace("%2F", "/");

                String paramStr = url.substring(url.indexOf('?') + 1);
                if ((paramStr != null) && (paramStr.length() > 0))
                {
//                    paramStr = URLEncodedUtils.format(URLEncodedUtils.parse(URI.create(paramStr), "UTF-8"), "UTF-8");
                    url = url.substring(0, url.indexOf("://")) + "://" + uri + "?" + paramStr;
                }
                else
                {
                    url = url.substring(0, url.indexOf("://")) + "://" + uri;
                }
            }
            else
            {
                String uri = url.substring(url.indexOf("://") + 3);
                uri = URLEncoder.encode(uri, "UTF-8");
                uri = uri.replace("%2F", "/");
                url = url.substring(0, url.indexOf("://")) + "://" + uri;
            }
            url = url.trim().replace(" ", "%20");
            return url.trim().replace("+", "%20");
        }
        catch (Exception e) {}
        return reutrnUrl;
    }

    public static String sqliteEscape(String keyWord)
    {
        keyWord = keyWord.replace("/", "//");
        keyWord = keyWord.replace("'", "''");
        keyWord = keyWord.replace("[", "/[");
        keyWord = keyWord.replace("]", "/]");
        keyWord = keyWord.replace("%", "/%");
        keyWord = keyWord.replace("&", "/&");
        keyWord = keyWord.replace("_", "/_");
        keyWord = keyWord.replace("(", "/(");
        keyWord = keyWord.replace(")", "/)");
        return keyWord;
    }

    public static List<Integer> integerArrayConvertList(Integer[] paramArray)
    {
        if ((paramArray == null) || (paramArray.length < 1)) {
            return null;
        }
        List<Integer> paramList = new ArrayList();
        for (Integer param : paramArray) {
            paramList.add(param);
        }
        return paramList;
    }

    public static String[] listConvertStringArray(List<String> paramList)
    {
        if ((paramList == null) || (paramList.size() < 0)) {
            return null;
        }
        String[] paramArray = new String[paramList.size()];
        for (int index = 0; index < paramList.size(); index++) {
            paramArray[index] = ((String)paramList.get(index));
        }
        return paramArray;
    }

    public static long getByteCountFromStr(String str)
    {
        if (TextUtils.isEmpty(str)) {
            return -1L;
        }
        long size = -1L;
        int unitIndex = -1;
        long unitSize = -1L;
        str = str.toUpperCase(Locale.ENGLISH);
        if (str.endsWith("KB"))
        {
            unitIndex = str.lastIndexOf("KB");
            unitSize = 1024L;
        }
        else if (str.endsWith("MB"))
        {
            unitIndex = str.lastIndexOf("MB");
            unitSize = (long) Math.pow(1024.0D, 2.0D);
        }
        else if (str.endsWith("GB"))
        {
            unitIndex = str.lastIndexOf("GB");
            unitSize = (long) Math.pow(1024.0D, 3.0D);
        }
        else if (str.endsWith("B"))
        {
            unitIndex = str.lastIndexOf("B");
            unitSize = 1L;
        }
        if (unitIndex != -1)
        {
            String fStr = str.substring(0, unitIndex);
            try
            {
                float num = Float.parseFloat(fStr);
                size = (long) (num * (float)unitSize);
            }
            catch (NumberFormatException localNumberFormatException) {}
        }
        return size;
    }

    public static boolean isEmpty(String str)
    {
        return (TextUtils.isEmpty(str)) || (TextUtils.isEmpty(str.trim()));
    }
}
